package com.lfy.modules.monitor.controller;

import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.config.thread.MyLinkedBlockingQueue;
import com.lfy.modules.common.config.thread.ThreadPoolConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@Slf4j
public class TestController {
    @Resource(name="mainThreadPool")
    private ThreadPoolExecutor mainThreadPool;
    @GetMapping("/test")
    public R<Void> test(){
      log.info("线程外");
        CompletableFuture.runAsync(()->{
            log.info("内部线程");
        },mainThreadPool);
        //核心线程数
        log.info("核心线程数：{}",mainThreadPool.getCorePoolSize());
        log.info("最大线程数：{}",mainThreadPool.getMaximumPoolSize());
        log.info("当前线程数：{}",mainThreadPool.getPoolSize());
        log.info("当前线程数：{}",mainThreadPool.getPoolSize());
        log.info("活跃线程数：{}",mainThreadPool.getActiveCount());
        log.info("同时进入线程池中的最大线程数：{}",mainThreadPool.getLargestPoolSize());
        log.info("线程池中执行任务的数量：{}",mainThreadPool.getCompletedTaskCount());
        log.info("当前负载：{}", mainThreadPool.getActiveCount()/mainThreadPool.getMaximumPoolSize());
        log.info("峰值负载：{}", mainThreadPool.getLargestPoolSize()/mainThreadPool.getMaximumPoolSize());
      MyLinkedBlockingQueue queue1 = (MyLinkedBlockingQueue) mainThreadPool.getQueue();
      log.info("队列大小：{}",queue1.getCapacity());
        log.info("队列元素个数：{}", mainThreadPool.getQueue().size());
        log.info("队列类型：{}", mainThreadPool.getQueue().getClass().getName());
        log.info("队列剩余容量：{}", mainThreadPool.getQueue().remainingCapacity());

        ThreadPoolConfig.CORE_SIZE = 8;
        ThreadPoolConfig.MAX_SIZE = 16;
        mainThreadPool.setCorePoolSize(ThreadPoolConfig.CORE_SIZE);
        mainThreadPool.setMaximumPoolSize(ThreadPoolConfig.MAX_SIZE);
        MyLinkedBlockingQueue<Runnable> queue = (MyLinkedBlockingQueue<Runnable>) mainThreadPool.getQueue();
        queue.setCapacity(300);
        log.info("核心线程数：{}",mainThreadPool.getCorePoolSize());
        log.info("最大线程数：{}",mainThreadPool.getMaximumPoolSize());
        log.info("当前线程数：{}",mainThreadPool.getPoolSize());
        log.info("当前线程数：{}",mainThreadPool.getPoolSize());
        log.info("活跃线程数：{}",mainThreadPool.getActiveCount());
        log.info("同时进入线程池中的最大线程数：{}",mainThreadPool.getLargestPoolSize());
        log.info("线程池中执行任务的数量：{}",mainThreadPool.getCompletedTaskCount());
        log.info("当前负载：{}", mainThreadPool.getActiveCount()/mainThreadPool.getMaximumPoolSize());
        log.info("峰值负载：{}", mainThreadPool.getLargestPoolSize()/mainThreadPool.getMaximumPoolSize());
        log.info("队列元素个数：{}", mainThreadPool.getQueue().size());
        log.info("队列类型：{}", mainThreadPool.getQueue().getClass().getName());
        log.info("队列剩余容量：{}", mainThreadPool.getQueue().remainingCapacity());
        return R.success();
    }
    @GetMapping("/tttt")
  public void test2(){
      mainThreadPool.execute(()->{
        try {
          log.info("test");
          Thread.sleep(10000);
          log.info("1");
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      });
    }
    @GetMapping("/pdf/test")
    public void test3(){

    }
}
